package com.zzr.demo.springsecurity.seven.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: xiaomi
 * @date: 2021/6/22
 * @description:
 */
@Slf4j
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {


    /**
     * 1. 查询 DB
     * 2.
     *
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws
            UsernameNotFoundException {
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("admins,ROLE_sale");
        //auths 不能传 null
        return new User("lucy", new BCryptPasswordEncoder().encode("123"), auths);

    }
}
